using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;
public partial class Admin_EmailTemplate : System.Web.UI.Page
{
    #region Region
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    protected string hidColSort
    {
        get
        {
            object o = ViewState["hidColSort"];
            if (o == null) return "[" + ADESSA.Entities.Newsletter._Table + "].[" + NewsletterColumns.Order + "]";
            return o.ToString();
        }
        set
        {
            ViewState["hidColSort"] = value;
        }
    }
    public string Title
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["title"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["title"];
        }
    }
    public string CreDate
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["credate"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["credate"];
        }
    }
    public string UpDate
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["update"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["update"];
        }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        LoadCurent();
        LoadData();
    }
    #region LoadCurent
    private void LoadCurent()
    {
        HtmlGenericControl ulEmail = (HtmlGenericControl)Master.FindControl("ulEmail");
        ulEmail.Attributes.Add("class", "current");
        HtmlGenericControl liSubShowEmail = (HtmlGenericControl)Master.FindControl("liSubShowEmail");
        liSubShowEmail.Attributes.Add("class", "sub_show");
        HtmlControl divSelectSubEmail = (HtmlControl)Master.FindControl("divSelectSubEmail");
        divSelectSubEmail.Attributes.Add("class", "select_sub show");
    }
    #endregion
    #region LoadData

    private void LoadData()
    {
        Criteria cri = new Criteria();
        hyperlinkPager.PageSize = RTISettings.PageSize;
        NoCountStart = (hyperlinkPager.PageSize * (hyperlinkPager.CurrentIndex - 1)) + 1;
        int totalrecord = 0;
        if (!string.IsNullOrEmpty(this.Title))
            cri.add("[" + Newsletter._Table + "].[" + NewsletterColumns.Title + "]", this.Title, Condition.LIKE, Condition.AND);
        if (!string.IsNullOrEmpty(this.CreDate))
            cri.addShortDate("[" + Newsletter._Table + "].[" + NewsletterColumns.CreatedDate + "]", Utility.ParseDateTime(this.CreDate), Condition.EQUAL, Condition.AND);
        if (!string.IsNullOrEmpty(this.UpDate))
            cri.addShortDate("[" + Newsletter._Table + "].[" + NewsletterColumns.UpdatedDate + "]", Utility.ParseDateTime(this.UpDate), Condition.EQUAL, Condition.AND);
        NewsletterCollection _NewsletterCollection = NewsletterService.GetNewsletterListByParam(hidColSort, OrderDirection.ASC, hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out totalrecord);

        rptList.DataSource = _NewsletterCollection;
        rptList.DataBind();
        hyperlinkPager.ItemCount = totalrecord;
        if ((float)totalrecord / (float)hyperlinkPager.PageSize <= 1)
            hyperlinkPager.Visible = false;

    }
    protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Newsletter _Newsletter = (Newsletter)e.Item.DataItem;
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            HtmlTableRow tr = (HtmlTableRow)e.Item.FindControl("tr");
            Literal ltrTitle = (Literal)e.Item.FindControl("ltrTitle");
            Literal ltrCreDate = (Literal)e.Item.FindControl("ltrCreDate");
            Literal ltrUpDate = (Literal)e.Item.FindControl("ltrUpDate");
            HtmlInputText txtOrder = (HtmlInputText)e.Item.FindControl("txtOrder");

            ltrTitle.Text = _Newsletter.Title;
            ltrCreDate.Text = Utility.GetFormattedDate(_Newsletter.CreatedDate);
            ltrUpDate.Text = Utility.GetFormattedDate(_Newsletter.UpdatedDate);
            txtOrder.Value = _Newsletter.Order.ToString();
            hidID.Value = _Newsletter.NewsletterId.ToString();
            int STT = NoCountStart + e.Item.ItemIndex;
            if (STT % 2 != 0)
            {
                tr.Attributes["class"] = "alternate-row";
            }
            else
            {
                tr.Attributes["class"] = "none";
            }
        }

    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        hyperlinkPager.CurrentIndex = currnetPageIndx;
        LoadData();
    }
    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string commandName = e.CommandName;
        object obj = new object();
        switch (commandName)
        {
            case "sortName":
                obj = "[" + Newsletter._Table + "].[" + NewsletterColumns.Title + "]";
                break;
            case "SortOrder":
                obj = "[" + Newsletter._Table + "].[" + NewsletterColumns.Order + "]";
                break;
            default:
                break;
        }
        if (OrderDirection.ASC == OrderDirection.ASC)
            Session["Order"] = OrderDirection.DESC;
        else
            Session["Order"] = OrderDirection.ASC;
        this.hidColSort = obj.ToString();
        LoadData();
    }
    #endregion
    #region Delete Account
    protected void lndelete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            CheckBox chkDelete = (CheckBox)item.FindControl("chkDelete");
            if (chkDelete.Checked == true)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                string strhidID = hidID.Value;
                int ID = int.Parse(strhidID);

                try
                {
                    NewsletterService.DeleteNewsletter(ID);

                }
                catch
                {
                    lb_error.Text = "Dont's delete this item because it is getting using another items";
                }
            }
        }
        LoadData();
    }
    #endregion
    #region Update
    protected void linkUpdate_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            HiddenField hidID = (HiddenField)item.FindControl("hidID");


            HtmlInputText txtOrder = (HtmlInputText)item.FindControl("txtOrder");
            Newsletter _Newsletter = NewsletterService.GetNewsletter(int.Parse(hidID.Value));


            if (_Newsletter != null)
            {
                if (!string.IsNullOrEmpty(txtOrder.Value.Trim()))
                    _Newsletter.Order = int.Parse(txtOrder.Value.Trim());
                NewsletterService.UpdateNewsletter(_Newsletter);
            }
        }
        LoadData();
    }
    #endregion
    #region Search
    protected void linkSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("EmailTemplate.aspx?title={0}&credate={1}&update={2}", txtTitleSearch.Text.Trim(), txtCreDateSearch.Value.Trim(), txtUpDateSearch.Value.Trim()));
    }
    protected void linkSearchAll_Click(object sender, EventArgs e)
    {
        Response.Redirect("EmailTemplate.aspx");
    }

        #endregion



}
